<?php
/**
 * Created by PhpStorm.
 * User: hcadmin
 * Date: 17/1/4
 * Time: 下午12:08
 */
namespace app\controllers;

use app\models\News;
use yii\web\Controller;

class NewsController extends Controller
{

    public function init()
    {
        return $this->enableCsrfValidation = false;
    }

    public function actionIndex()
    {
        $view = \Yii::$app->view;
        $view->params['menuItem'] = 2;


        return $this->render('index');
    }

    //发布页面
    public function actionPublish()
    {
        $view = \Yii::$app->view;
        $view->params['menuItem'] = 3;

        return $this->render('publish');
    }

    public function actionUploader()
    {
        if ((($_FILES["img1"]["type"] == "image/gif")
                || ($_FILES["img1"]["type"] == "image/jpeg")
                || ($_FILES["img1"]["type"] == "image/png"))
            && ($_FILES["img1"]["size"] < 5120000))
        {
            if ($_FILES["img1"]["error"] > 0)
            {
//                echo "Return Code: " . $_FILES["img1"]["error"] . "<br />";
                echo '';
            }
            else
            {

                if (file_exists("upload/" . $_FILES["img1"]["name"]))
                {
                    $url='http://'.$_SERVER['SERVER_NAME'].'/guochuang/basic/web';
                    echo $url. "/upload/" . $_FILES["img1"]["name"];
                }
                else
                {

                    $url='http://'.$_SERVER['SERVER_NAME'].'/guochuang/basic/web';
//                    echo dirname($url);

                    move_uploaded_file($_FILES["img1"]["tmp_name"],
                        "upload/" . $_FILES["img1"]["name"]);
                    echo $url. "/upload/" . $_FILES["img1"]["name"];
                }
            }
        }
        else
        {
            echo "";
        }

    }

    public function actionNewsPublish(){
        $request=\Yii::$app->request;
        $newsPoster=$request->post('newsPoster');
        $newsTitle=$request->post('newsTitle');
        $newsDetail=$request->post('newsDetail');
        $create_time = date('Y-m-d H:i:s');
        $newsCategory='1';
        $newsModel=new News();
        $newsModel->news_poster=$newsPoster;
        $newsModel->news_title=$newsTitle;
        $newsModel->news_detail=$newsDetail;
        $newsModel->create_time=$create_time;
        $newsModel->news_category=$newsCategory;
        if($newsModel->save()>0){
            echo json_encode('1');
        }
        else{
            echo json_encode('0');
        }

    }







 }



